home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 510 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.9 KB  |  97 lines

  1. Path: chronicle.mti.sgi.com!austern
  2. From: boukanov@kvark.fi.uib.no (Igor Boukanov)
  3. Newsgroups: comp.std.c++
  4. Subject: Namespace extension
  5. Date: 16 Feb 1996 09:06:50 PST
  6. Organization: Fysisk institutt, Universitetet i Bergen
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <4g2aj2$bko@ugress.uib.no>
  9. NNTP-Posting-Host: isolde.mti.sgi.com
  10. Summary: I propose to let template nemespaces.
  11. Keywords: C++, namespace, template
  12. X-Original-Date: 16 Feb 1996 16:15:30 GMT
  13. X-Newsreader: TIN [version 1.2 PL2]
  14. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  15.     iQBVAwUBMSS5uEy4NqrwXLNJAQFqTwIAjZn7b/50m+wrJg6bvvH3LEDdtqtlNHe3
  16.     Z2RfIlngApPCE5CLASSgHmS7Vkzwyq6QZjTidte6CApaarUvQzCRMw==
  17.     =ZeXn
  18. Originator: austern@isolde.mti.sgi.com
  19.  
  20.    Of cause, probably my proposal is of time, but namespaces are quite 
  21. new C++ feature, and only now I realize how to make it more flexible... 
  22.  
  23. So I suggest to let template namespaces. In this case it will be possible 
  24. to write something like this:
  25.  
  26. // declaration
  27. template<class T> namespace containers {
  28.    class simple_vector {
  29.       private:
  30.          T* data;
  31.          unsigned count;
  32.       public:
  33.          simple_vector(unsigned acount);
  34.          T& operator[](unsigned i);
  35.          .. // other methods  
  36.    };
  37.    
  38.    class simple_list{
  39.       ... // data members and methods
  40.    };
  41. }
  42.  
  43. ...
  44.  
  45. //implementations
  46. template<class T> namespace containers {
  47.   
  48.    simple_vector::simple_vector(unsigned acount) {
  49.       count = acount;
  50.       data = new T[acount];
  51.    }
  52.    
  53.    T* simple_vector::operator[](unsigned i) { return data[i]; }  
  54.   
  55.    ... // methods for simple_list;
  56. }
  57.  
  58. And now how to use it:
  59.  
  60. void example_of_using_1(){
  61.    containers<int>::simple_vector v(10);
  62.    containers<float>::simple_list v(10);
  63. };
  64.  
  65.  
  66. void example_of_using_2(){
  67.    using namespace containers<char>;
  68.    simple_vector char_vector(100);
  69. };
  70.  
  71.  
  72. I think there are at least next reasons to introduce this extension.
  73.   
  74.   1. It can make declarations and implementations of template libraries much 
  75. more clear because one does not need to write template keyword and something
  76. like <T> everywhere. (Just look at the example: I used "template" word only 
  77. twice and I never used <T> inside containers namespace.)
  78.   
  79.   2. It will be much more easy to instant such library for particular 
  80. template parameters. 
  81. (For a example, containers from example can be instanted by single line: 
  82. template namespace containers<bool>; 
  83. instead of such line for each template class.)
  84.   
  85.   3. It makes C++  easy to study, because somebody will have to know only 
  86. the general idea about templates and how to apply them to any C++ elements
  87. (function, class, namespase,...).  
  88.   
  89.    So, what do you think about this?     
  90.  
  91. --
  92. Regards, Igor Boukanov (igor.boukanov@fi.uib.no).
  93. ---
  94. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  95.   Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy is
  96.   in http://reality.sgi.com/employees/austern_mti/std-c++/policy.html. ]
  97.